//
//  HouseInfoControlView.m
//  宜居HD
//
//  Created by 张金国 on 16/4/28.
//  Copyright © 2016年 张金国. All rights reserved.
//

#import "HouseInfoControlView.h"
#import "TitleView.h"
#import "Masonry.h"
#import "SearchView.h"
#import "Macro.h"
#import "UIButton+ColorandText.h"
#import "UIColor+RandomColor.h"
@implementation HouseInfoControlView
#pragma mark - init
- (instancetype)initWithHouseInfoControlViewBlock:(HouseInfoControlViewBlock)houseInfoControlViewBlock {
    if (self = [super init]) {
        self.houseInfoControlViewBlock = houseInfoControlViewBlock;
        [self setupConstraints];
    }
    return self;
}
- (void)setupConstraints{
    {
        TitleView *titleView4 = [[TitleView alloc]initWithLineColor:[UIColor redColor] title:@"搜索房源并控制" titleFont:kLabelTextColor];
        [self addSubview:titleView4];
        [titleView4 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.equalTo(self).offset(10);
            make.height.equalTo(self.mas_height).multipliedBy(1/4.0f);
            make.width.equalTo(self.mas_width).multipliedBy(1/4.0f);
        }];
        UILabel *subtitle = [[UILabel alloc]init];
        subtitle.text = @"精确房源搜索";
        [self addSubview:subtitle];
        [subtitle mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(titleView4.mas_bottom);
            make.left.equalTo(titleView4.mas_left);
        }];
        
        SearchView *tempsearchView = nil;
        
        for (int i = 0 ; i < 4 ; i ++) {
            SearchView *searchView = [[SearchView alloc]initWithTitle:@"栋号" titleFont:[UIFont preferredFontForTextStyle:UIFontTextStyleTitle3] cornerRadius:5.0f backgroundColor:[UIColor whiteColor]];
            [self addSubview:searchView];
            
            if (tempsearchView == nil) {
                [searchView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.top.equalTo(subtitle.mas_bottom).offset(10);
                    make.left.equalTo(subtitle.mas_left);
                    make.width.equalTo(self.mas_width).multipliedBy(1/5.40f);
                    make.height.equalTo(self.mas_height).multipliedBy(1/8.0f);
                }];
            }else {
                [searchView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.top.equalTo(subtitle.mas_bottom).offset(10);
                    make.left.equalTo(tempsearchView.mas_right).offset(20);
                    make.width.equalTo(self.mas_width).multipliedBy(1/5.40f);
                    make.height.equalTo(self.mas_height).multipliedBy(1/8.0f);
                }];
                
            }
            tempsearchView = searchView;
            NSString *text = searchView.text;
            text = (NSString *)(text == nil?[NSNull null]:text);
            [self.houseNumSearchArray addObject:text];
            
            if (i == 0) {
                UILabel *subtitle1 = [[UILabel alloc]init];
                subtitle1.text = @" 按条件搜索(可多选)";
                [self addSubview:subtitle1];
                [subtitle1 mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.top.equalTo(searchView.mas_bottom).offset(10);
                    make.left.equalTo(searchView.mas_left);
                }];
                
                SearchView *tempsearchView2 = nil;
                for (int j = 0; j < 4; j++) {
                    SearchView *searchView2 = [[SearchView alloc]initWithTitle:@"栋号" titleFont:[UIFont preferredFontForTextStyle:UIFontTextStyleTitle3] cornerRadius:5.0f backgroundColor:[UIColor whiteColor]];
                    [self addSubview:searchView2];
                    if (tempsearchView2 == nil) {
                        [searchView2 mas_makeConstraints:^(MASConstraintMaker *make) {
                            make.top.equalTo(subtitle1.mas_bottom).offset(10);
                            make.left.equalTo(subtitle1.mas_left);
                            make.width.equalTo(self.mas_width).multipliedBy(1/5.40f);
                            make.height.equalTo(self.mas_height).multipliedBy(1/8.0f);
                        }];
                    }else {
                        [searchView2 mas_makeConstraints:^(MASConstraintMaker *make) {
                            make.top.equalTo(subtitle1.mas_bottom).offset(10);
                            make.left.equalTo(tempsearchView2.mas_right).offset(20);
                            make.width.equalTo(self.mas_width).multipliedBy(1/5.40f);
                            make.height.equalTo(self.mas_height).multipliedBy(1/8.0f);
                        }];
                    }
                    
                    tempsearchView2 = searchView2;
                    
                    if (j == 0) {
                        SearchView *tempsearchView3 = nil;
                        for (int k = 0 ; k < 4; k ++) {
                            SearchView *searchView3 = [[SearchView alloc]initWithTitle:@"栋号" titleFont:[UIFont preferredFontForTextStyle:UIFontTextStyleTitle3] cornerRadius:5.0f backgroundColor:[UIColor whiteColor]];
                            [self addSubview:searchView3];
                            if (tempsearchView3 == nil) {
                                [searchView3 mas_makeConstraints:^(MASConstraintMaker *make) {
                                    make.top.equalTo(searchView2.mas_bottom).offset(20);
                                    make.left.equalTo(searchView2.mas_left);
                                    make.width.equalTo(self.mas_width).multipliedBy(1/5.40f);
                                    make.height.equalTo(self.mas_height).multipliedBy(1/8.0f);
                                }];
                            }else {
                                [searchView3 mas_makeConstraints:^(MASConstraintMaker *make) {
                                    make.top.equalTo(tempsearchView3.mas_top);
                                    make.left.equalTo(tempsearchView3.mas_right).offset(20);
                                    make.width.equalTo(self.mas_width).multipliedBy(1/5.40f);
                                    make.height.equalTo(self.mas_height).multipliedBy(1/8.0f);
                                }];
                            }
                            tempsearchView3 = searchView3;
                            NSString *text = searchView3.text;
                            text = (NSString *)(text == nil?[NSNull null]:text);
                            [self.houseNumSearchArray addObject:text];

                            
                            if (k == 3) {
                                
                                UIButton *button3 = [UIButton buttonWithBackgroundColor:[UIColor randomColor] textFont:[UIFont preferredFontForTextStyle:UIFontTextStyleTitle3] textColor:[UIColor whiteColor] title:@"条件搜索" cornerRadius:5.0f];
                                button3.titleLabel.textColor = [UIColor blackColor];
                                [button3 addTarget:self action:@selector(handleButonEvent:) forControlEvents:UIControlEventTouchUpInside];
                                button3.tag = 1;
                                [self  addSubview:button3];
                                [button3 mas_makeConstraints:^(MASConstraintMaker *make) {
                                    make.top.equalTo(searchView3.mas_top);
                                    make.left.equalTo(searchView3.mas_right).offset(35);
                                    make.width.equalTo(self.mas_width).multipliedBy(1/10.0f);
                                    make.height.equalTo(searchView3.mas_height);
                                }];
                            }
                        }
                    }
                }
            }
            
            if (i == 3) {
                UIButton *button1 = [UIButton buttonWithBackgroundColor:[UIColor randomColor] textFont:[UIFont preferredFontForTextStyle:UIFontTextStyleTitle3] textColor:[UIColor whiteColor] title:@"房号搜索" cornerRadius:5.0f];
                [button1 addTarget:self action:@selector(handleButonEvent:) forControlEvents:UIControlEventTouchUpInside];
                button1.tag = 0;
                [self  addSubview:button1];
                [button1 mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.top.equalTo(searchView.mas_top);
                    make.left.equalTo(searchView.mas_right).offset(35);
                    make.width.equalTo(self.mas_width).multipliedBy(1/10.0f);
                    make.height.equalTo(searchView.mas_height);
                }];
            }
        }
    }

}

#pragma mark - Event response

- (void)handleButonEvent:(UIButton *)button {
    SearchButtonType searchButtonType = button.tag;
    self.houseInfoControlViewBlock(searchButtonType);
}
#pragma mark - Getters
- (NSMutableArray *)houseNumSearchArray {
    if (!_houseNumSearchArray) {
        _houseNumSearchArray = [NSMutableArray array];
    }
    return _houseNumSearchArray;
}
- (NSMutableArray *)conditionSearchArray {
    if (!_conditionSearchArray) {
        _conditionSearchArray = [NSMutableArray array];
    }
    return _conditionSearchArray;
}


@end
